package com.freaky.jx3.mjb;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MJBPegging extends Activity implements OnTouchListener,
		OnGestureListener {

	GestureDetector mGestureDetector;
	Button btnInquiry;
	EditText edtMjbNumber;
	TextView txtShow;
	LinearLayout linPegging;
	
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mjbpegging);
		mGestureDetector = new GestureDetector((OnGestureListener) this);
		linPegging = (LinearLayout) findViewById(R.id.linPegging);
		linPegging.setOnTouchListener(this);
		linPegging.setLongClickable(true);

		btnInquiry = (Button) findViewById(R.id.btnInquiry);
		edtMjbNumber = (EditText) findViewById(R.id.edtMjbNumber);
		txtShow = (TextView) findViewById(R.id.txtShow);

		// 初始化赋值
		edtMjbNumber.setText(BaseConstant.mjbnum);
		txtShow.setText(BaseConstant.show);

		btnInquiry.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (!edtMjbNumber.getText().toString().equals("")) {

					int number = Integer.valueOf(edtMjbNumber.getText()
							.toString());

					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.append("所需名剑队等级为：\n");
					stringBuilder.append("2V2:");
					stringBuilder.append(mjbPegging(number,
							BaseConstant.modulus2v2));
					stringBuilder.append("\n");
					stringBuilder.append("3V3:");
					stringBuilder.append(mjbPegging(number,
							BaseConstant.modulus3v3));
					stringBuilder.append("\n");
					stringBuilder.append("5V5:");
					stringBuilder.append(mjbPegging(number,
							BaseConstant.modulus5v5));
					stringBuilder.append("\n");
					txtShow.setText(stringBuilder.toString());

					// 赋值保存
					BaseConstant.mjbnum = "" + number;
					BaseConstant.show = stringBuilder.toString();
				}
				// 关闭输入法
				InputMethodManager m = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				m.hideSoftInputFromWindow(edtMjbNumber.getWindowToken(), 0);
			}
		});
		edtMjbNumber.addTextChangedListener(new EditTextMaxLengthWatcher(4,
				edtMjbNumber));
		
		


	}

	public int mjbPegging(int number, double modulus) {
		int teamFraction = 1;
		while (true) {
			teamFraction++;
			if (number <= BaseConstant.mjbCalculate(teamFraction, modulus)) {
				break;
			}
		}
		return teamFraction;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// 画面切换
		if (e1.getX() - e2.getX() > 50 && Math.abs(velocityX) > 0) {

			// Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
		} else if (e2.getX() - e1.getX() > 50 && Math.abs(velocityX) > 0) {
			Intent intent = new Intent();
			intent.setClass(MJBPegging.this, MJBActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			// Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();

		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub

		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return mGestureDetector.onTouchEvent(event);
	}

	// 返回键监听
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			System.exit(0);
			return false;
		}
		return false;
	}
}
